package com.powerteam.service.crm.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powerteam.exception.BusinessException;
import com.powerteam.mapper.crm.ActivityMapper;
import com.powerteam.model.crm.Activity;
import com.powerteam.service.crm.ActivityService;
import com.powerteam.vo.Result;
import com.powerteam.vo.crm.QueryActivityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 动态服务相关接口实现
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {


    private static final String CLASS_NAME = "ActivityServiceImpl";
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result insert(Activity activity) {

        try {

            // 创建动态信息
            activity.setCreateDate(new Date());
            boolean result = activityMapper.insert(activity) > 0;
            log.info("{} insert 创建动态信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(activity), result);

            return new Result(result);
        } catch (Exception e) {
            log.warn("{} insert 创建动态信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(activity));
            log.error(e.getMessage(), e);
            throw new BusinessException("创建动态信息错误");
        }
    }

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result deleteByResource(Activity activity) {

        try {

            // 根据资源信息删除动态信息
            boolean result = activityMapper.deleteByResource(activity) > 0;
            log.info("{} deleteByResource 根据资源信息删除动态信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(activity), result);

            return new Result(result);
        } catch (Exception e) {
            log.warn("{} deleteByResource 根据资源信息删除动态信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(activity));
            log.error(e.getMessage(), e);
            throw new BusinessException("删除动态信息错误");
        }
    }

    @Override
    public PageInfo<Activity> find(QueryActivityVO vo) {

        try {

            // 分页 如果不是禁用分页 设置分页条件
            if (StringUtils.isEmpty(vo.getDisablePaging()) || !vo.getDisablePaging()) {
                PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
            }
            // 分页查询动态信息
            List<Activity> activityList = activityMapper.find(vo);
            log.info("{} find 分页查询动态信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(vo), JSON.toJSON(activityList));

            return new PageInfo<>(activityList);
        } catch (Exception e) {
            log.warn("{} find 分页查询动态信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(vo));
            log.error(e.getMessage(), e);
            return new PageInfo<>(new ArrayList<>());
        }
    }

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result delete(Activity activity) {

        try {

            // 删除动态信息
            boolean result = activityMapper.delete(activity) > 0;
            log.info("{} delete 删除动态信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(activity), result);

            return new Result(result);
        } catch (Exception e) {
            log.info("{} delete 删除动态信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(activity));
            log.error(e.getMessage(), e);
            throw new BusinessException("删除动态信息异常错误");
        }

    }
}
